package com.adayo.memoryfree.fragment;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


import com.adayo.memoryfree.R;
import com.adayo.memoryfree.adapter.AutoStartAdapter;
import com.adayo.memoryfree.base.FragmentBase;
import com.adayo.memoryfree.bean.AutoStartInfo;
import com.adayo.memoryfree.utils.JBootStartKit;
import com.adayo.memoryfree.utils.JToastKit;
import com.adayo.memoryfree.utils.JRootKit;
import com.adayo.memoryfree.utils.JShellKit;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.fragment_auto_start)
public class AutoStartFragment extends FragmentBase {
	public static final int REFRESH_BT = 111;
	private static final String ARG_POSITION = "position";
	private int position; // 0:普通软件，2 系统软件
	AutoStartAdapter mAutoStartAdapter;

	@ViewInject(R.id.listview)
	ListView listview;

	@ViewInject(R.id.bottom_lin)
	LinearLayout bottom_lin;

	@ViewInject(R.id.disable_button)
	Button disableButton;
	@ViewInject(R.id.topText)
	TextView topText;
	List<AutoStartInfo> isSystemAuto = null;
	List<AutoStartInfo> noSystemAuto = null;
	private int canDisableCom;

	private Handler mHandler = new Handler() {


		public void handleMessage(Message msg) {
			switch (msg.what) {
				case REFRESH_BT:
					refeshButoom();
					break;
			}
		}
	};


	@Override
	protected void initWidgets() {
		position = getArguments().getInt(ARG_POSITION);
		fillData();
	}

	@Event(value = {R.id.disable_button})
	private void onViewClick(View v) {
		JRootKit.preparezlsu(context);
		disableAPP();
	}

	private void disableAPP() {
		List<String> mSring = new ArrayList<>();
		for (AutoStartInfo auto : noSystemAuto) {
			if (auto.isEnable()) {
				String packageReceiverList[] = auto.getPackageReceiver().toString().split(";");
				for (int j = 0; j < packageReceiverList.length; j++) {
					String cmd = "pm disable " + packageReceiverList[j];
					//部分receiver包含$符号，需要做进一步处理，用"$"替换掉$
					cmd = cmd.replace("$", "\"" + "$" + "\"");
					//执行命令
					mSring.add(cmd);
				}
			}
		}

		JShellKit.CommandResult mCommandResult = JShellKit.execCommand(mSring, true, true);
		if (mCommandResult.result == 0) {
			JToastKit.getInstance().showMessage("应用已经全部禁止");
			for (AutoStartInfo auto : noSystemAuto) {
				if (auto.isEnable()) {
					auto.setEnable(false);
				}
			}
			mAutoStartAdapter.notifyDataSetChanged();
			refeshButoom();
		} else {
			JToastKit.getInstance().showMessage("该功能需要获取系统root权限，请允许获取root权限");
		}
	}


	private void fillData() {
		if (position == 0) {
			topText.setText("禁止下列软件自启,可提升运行速度");
		} else {
			topText.setText("禁止系统核心软件自启,将会影响手机的正常使用,请谨慎操作");
		}
		List<AutoStartInfo> mAutoStartInfo = JBootStartKit.fetchAutoApps(context);
		noSystemAuto = new ArrayList<>();
		isSystemAuto = new ArrayList<>();
		for (AutoStartInfo a : mAutoStartInfo) {
			if (a.isSystem()) {
				isSystemAuto.add(a);
			} else {
				noSystemAuto.add(a);
			}
		}

		if (position == 0) {
			mAutoStartAdapter = new AutoStartAdapter(context, noSystemAuto, mHandler);
			listview.setAdapter(mAutoStartAdapter);
			refeshButoom();
		} else {
			mAutoStartAdapter = new AutoStartAdapter(context, isSystemAuto, null);
			listview.setAdapter(mAutoStartAdapter);
		}
	}

	private void refeshButoom() {
		if (position == 0) {
			canDisableCom = 0;
			for (AutoStartInfo autoS : noSystemAuto) {
				if (autoS.isEnable()) {
					canDisableCom++;
				}
			}
			if (canDisableCom > 0) {
				bottom_lin.setVisibility(View.VISIBLE);
				disableButton.setText("可优化" + canDisableCom + "款");
			} else {
				bottom_lin.setVisibility(View.GONE);
			}
		}
	}
}
